!
!  listing of derived quantities extracted from calibrated images
!
!  Copyright © 2011 F.Hroch (hroch@physics.muni.cz)
!
!  This file is part of Munipack.
!
!  Munipack is free software: you can redistribute it and/or modify
!  it under the terms of the GNU General Public License as published by
!  the Free Software Foundation, either version 3 of the License, or
!  (at your option) any later version.
!  
!  Munipack is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU General Public License for more details.
!  
!  You should have received a copy of the GNU General Public License
!  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.


program list

  use mlist
  use muniarrays
  

  implicit none
  
  integer, parameter :: dbl = selected_real_kind(15)
  integer, parameter :: clen = 4096
  integer, parameter :: memstep = 1000
  character(len=clen) :: filename,line,record,output!,c
  character(len=9) :: dateobs = 'DATE-OBS',time = 'TIME', exptime = 'EXPTIME', &
       filter = 'FILTER'
  character(len=10) :: column = 'apflux1', key
!  character(len=10), dimension(35) :: columns
  ! the len is determined by TFORM, the dimension same as in muniphot/ifits
  real(dbl), dimension(:), allocatable :: xcoo, ycoo, vxcoo, vycoo
  character(len=10), dimension(:), allocatable :: hkeys
  integer, dimension(:), allocatable :: idx
  real(dbl) :: tol = 10.0, long, lat = -99.9, alpha = -1.0, delta, &
       min0, per = -1.0, x, y, vx, vy, jd0
  logical :: relative = .true., mag = .false., full = .false., &
       positions = .false., header = .false.
  integer :: i,eq,cc,istat,quiet=0,ncoo,nidx,nkeys !,ncolumns

!  ncolumns = 0
  ncoo = 0
  nidx = 0
  nkeys = 0
  allocate(xcoo(memstep),ycoo(memstep),vxcoo(memstep),vycoo(memstep))
  allocate(idx(memstep))
  allocate(hkeys(memstep))

  vxcoo = 0.0_dbl
  vycoo = 0.0_dbl

  do
     read(*,'(a)',end=99,iostat=istat) record
     if( istat /= 0 ) then
        write(*,*) 'Unexpected string on input.'
        stop 21
     end if

     cc = index(record,'#')
     if( cc == 0 ) then
        line = record
     else
        line = record(1:cc-1)
     end if

     eq = index(line,'=')

     if( index(line,'OUTPUT') > 0 .and. eq > 0) then
        
        read(line(eq+1:),*,iostat=istat) output
        if( istat /= 0 ) then
           write(*,*) 'OUTPUT parameter read failed.'
           stop 21
        end if

     else if( index(line,'INDEX') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) i
        if( istat /= 0 ) then
           write(*,*) 'INDEX parameter read failed.'
           stop 21
        end if

        if( nidx == size(idx) ) then
           call reallocate(idx,size(idx)+memstep)
        end if

        nidx =  nidx + 1
        idx(nidx) = i

     else if( index(line,'RECT') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) x,y,vx,vy
        if( istat /= 0 ) then
           write(*,*) 'RECT parameter read failed.'
           stop 21
        end if

        if( ncoo == size(xcoo) ) then
           call reallocate(xcoo,size(xcoo)+memstep)
           call reallocate(ycoo,size(ycoo)+memstep)
           call reallocate(vxcoo,size(vxcoo)+memstep)
           call reallocate(vycoo,size(vycoo)+memstep)
        end if

        ncoo =  ncoo + 1
        xcoo(ncoo) = x
        ycoo(ncoo) = y
        vxcoo(ncoo) = vx
        vycoo(ncoo) = vy

     else if( index(line,'KEYWORD') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) key
        if( istat /= 0 ) then
           write(*,*) 'KEYWORD parameter read failed.'
           stop 21
        end if

        if( nkeys == size(hkeys) ) then
           call reallocate(hkeys,size(hkeys)+memstep)
        end if

        nkeys =  nkeys + 1
        hkeys(nkeys) = key

     else if( index(line,'TOL') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) tol
        if( istat /= 0 ) then
           write(*,*) 'TOL parameter read failed.'
           stop 21
        end if

     else if( index(line,'JD0') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) jd0
        if( istat /= 0 ) then
           write(*,*) 'JD0 parameter read failed.'
           stop 21
        end if

!     else if( index(line,'MRECT') > 0 .and. eq > 0) then
!
!        read(line(eq+1:),*,iostat=istat) x,y
!        if( istat /= 0 ) then
!           write(*,*) 'MRECT parameter read failed.'
!           stop 21
!        end if
!
!        if( ncoo == size(xcoo) ) then
!           call reallocate(xcoo,size(xcoo)+memstep)
!           call reallocate(ycoo,size(ycoo)+memstep)
!        end if
!
!        mcoo =  mcoo + 1
!        mcoo(mcoo) = x;
!        mcoo(mcoo) = y;
!

     else if( index(line,'COLUMN') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) column
        if( istat /= 0 ) then
           write(*,*) 'COLUMN parameter read failed.'
           stop 21
        end if
!        ncolumns = ncolumns + 1
!        columns(ncolumns) = c

     else if( index(line,'HEADER') > 0 .and. eq > 0) then

        header = .true.

     else if( index(line,'MAG') > 0 .and. eq > 0) then

        mag = .true.

     else if( index(line,'RELATIVE') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) relative
        if( istat /= 0 ) then
           write(*,*) 'RELATIVE parameter read failed.'
           stop 21
        end if

     else if( index(line,'FULL_LISTING') > 0 .and. eq > 0) then

        full = .true.
        relative = .false.

     else if( index(line,'POSITION_LISTING') > 0 .and. eq > 0) then

        positions = .true.
        relative = .false.

     else if( index(line,'DEBUG') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) quiet
        if( istat /= 0 ) then
           write(*,*) 'DEBUG parameter read failed.'
           stop 21
        end if

     else if( index(line,'GEOGRAPHICAL') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) long,lat
        if( istat /= 0 ) then
           write(*,*) 'GEOGRAPHICAL parameter read failed.'
           stop 21
        end if

     else if( index(line,'EQUATORIAL') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) alpha,delta
        if( istat /= 0 ) then
           write(*,*) 'EQUATORIAL parameter read failed.'
           stop 21
        end if

     else if( index(line,'LC_ELEMENTS') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) min0, per
        if( istat /= 0 ) then
           write(*,*) 'LC_ELEMENTS parameter read failed.'
           stop 21
        end if

     else if( index(line,'DATE-OBS') > 0 .and. eq > 0 ) then
        read(line(eq+1:),*) dateobs

     else if( index(line,'TIME') > 0 .and. eq > 0 ) then
        read(line(eq+1:),*) time

     else if( index(line,'EXPTIME') > 0 .and. eq > 0 ) then
        read(line(eq+1:),*) exptime

     else if( index(line,'FILTER') > 0 .and. eq > 0 ) then
        read(line(eq+1:),*) filter

     else if( index(line,'FILE') > 0 .and. eq > 0) then

        read(line(eq+1:),*,iostat=istat) filename
        if( istat /=0 ) then
           write(*,*) 'Invalid filename format.'
           stop 21
        end if

        call munilist(filename,output,column,idx(:nidx), &
             xcoo(:ncoo),ycoo(:ncoo),vxcoo(:ncoo),vycoo(:ncoo),tol, &
             min0,per,long,lat,alpha,delta,jd0,relative,mag,full,positions, &
             header,quiet,hkeys(:nkeys),(/dateobs,time,exptime,filter/))

     end if

     
  end do
99 continue


  deallocate(xcoo,ycoo,vxcoo,vycoo,idx)

  stop 0

end program list
